<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgpu - volumetric perlin</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="example.css">
	</head>

	<body>

		<div id="info">
			<a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>

			<div class="title-wrapper">
				<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Volumetric Perlin</span>
			</div>

			<small>Volumetric rendering using raymarching and 3D Perlin noise texture.</small>
		</div>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.webgpu.js",
					"three/webgpu": "../build/three.webgpu.js",
					"three/tsl": "../build/three.tsl.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three/webgpu';
			import { Break, If, vec3, vec4, texture3D, uniform, Fn } from 'three/tsl';

			import { RaymarchingBox } from 'three/addons/tsl/utils/Raymarching.js';

			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
			import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js';

			import { Inspector } from 'three/addons/inspector/Inspector.js';

			let renderer, scene, camera;
			let mesh;

			init();

			function init() {

				renderer = new THREE.WebGPURenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setAnimationLoop( animate );
				renderer.inspector = new Inspector();
				document.body.appendChild( renderer.domElement );

				scene = new THREE.Scene();

				camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
				camera.position.set( 0, 0, 2 );

				new OrbitControls( camera, renderer.domElement );

				// Texture

				const size = 128;
				const data = new Uint8Array( size * size * size );

				let i = 0;
				const perlin = new ImprovedNoise();
				const vector = new THREE.Vector3();

				for ( let z = 0; z < size; z ++ ) {

					for ( let y = 0; y < size; y ++ ) {

						for ( let x = 0; x < size; x ++ ) {

							vector.set( x, y, z ).divideScalar( size );

							const d = perlin.noise( vector.x * 6.5, vector.y * 6.5, vector.z * 6.5 );

							data[ i ++ ] = d * 128 + 128;

						}

					}

				}

				const texture = new THREE.Data3DTexture( data, size, size, size );
				texture.format = THREE.RedFormat;
				texture.minFilter = THREE.LinearFilter;
				texture.magFilter = THREE.LinearFilter;
				texture.unpackAlignment = 1;
				texture.needsUpdate = true;

				// Shader

				const opaqueRaymarchingTexture = Fn( ( { texture, steps, threshold } ) => {

					const finalColor = vec4( 0 ).toVar();

					RaymarchingBox( steps, ( { positionRay } ) => {

						const mapValue = texture.sample( positionRay.add( 0.5 ) ).r.toVar();

						If( mapValue.greaterThan( threshold ), () => {

							const p = vec3( positionRay ).add( 0.5 );

							finalColor.rgb.assign( texture.normal( p ).mul( 0.5 ).add( positionRay.mul( 1.5 ).add( 0.25 ) ) );
							finalColor.a.assign( 1 );
							Break();

						} );

					} );

					return finalColor;

				} );

				//

				const threshold = uniform( 0.6 );
				const steps = uniform( 200 );

				const material = new THREE.NodeMaterial();
				material.colorNode = opaqueRaymarchingTexture( {
					texture: texture3D( texture, null, 0 ),
					steps,
					threshold
				} );
				material.side = THREE.BackSide;
				material.transparent = true;

				mesh = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 1 ), material );
				scene.add( mesh );

				//

				const gui = renderer.inspector.createParameters( 'Parameters' );
				gui.add( threshold, 'value', 0, 1, 0.01 ).name( 'threshold' );
				gui.add( steps, 'value', 0, 300, 1 ).name( 'steps' );

				window.addEventListener( 'resize', onWindowResize );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
